package com.example.user_module.login;

import com.example.common.entity.BaseEntity;
import com.example.common.mvvm.model.BaseRespository;
import com.example.user_module.room.UserDao;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.functions.Function;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginModel extends BaseRespository {

    Observable<BaseEntity>insterUser(String avatar,
                                     String description,
                                     long expires_time,
                                     Integer followCount,
                                     Integer likeCount,
                                     String name,
                                     String qqOpenId,
                                     Integer topCommentCount){
        Retrofit retrofit=new Retrofit.Builder()
                .client(new OkHttpClient.Builder()
                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                        .build())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://118.195.161.134:8088/")
                .build();
        Observable<RoomUserEntity> roomUserEntityObservable = retrofit.create(ApiService.class).requestUser(avatar, description, expires_time,
                followCount, likeCount, name, qqOpenId, topCommentCount);
        return roomUserEntityObservable.map(new Function<RoomUserEntity, BaseEntity>() {
            @Override
            public BaseEntity apply(RoomUserEntity roomUserEntity) throws Exception {
                return roomUserEntity;
            }
        });
    }

    public Observable<BaseEntity> addRoom(UserDao dao, RoomUserEntity.InsertUserValuesEntity entity){
        Observable<BaseEntity> observable = new Observable<BaseEntity>() {
            @Override
            protected void subscribeActual(Observer<? super BaseEntity> observer) {
                dao.insert(entity);

                IsLoginEnity isLoginEnity = new IsLoginEnity();
                isLoginEnity.message = "插入成功";
                isLoginEnity.status = 200;
                observer.onNext(isLoginEnity);
                observer.onComplete();
            }
        };
        return checkNetWork(observable);
    }

}
